#include "mainwindow.h"
#include "ui_mainwindow.h"


#define POLLING_INTERVAL 1000
#define CELL_BAR_MIN     3000
#define CELL_BAR_MAX     4200

#define MAX(x,y)   (((x)>(y)) ? (x) : (y))
#define MIN(x,y)   (((x)<(y)) ? (x) : (y))


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->cellPB_1->setMinimum(CELL_BAR_MIN);
    ui->cellPB_2->setMinimum(CELL_BAR_MIN);
    ui->cellPB_3->setMinimum(CELL_BAR_MIN);
    ui->cellPB_4->setMinimum(CELL_BAR_MIN);
    ui->cellPB_5->setMinimum(CELL_BAR_MIN);
    ui->cellPB_6->setMinimum(CELL_BAR_MIN);
    ui->cellPB_7->setMinimum(CELL_BAR_MIN);
    ui->cellPB_8->setMinimum(CELL_BAR_MIN);

    ui->cellPB_1->setMaximum(CELL_BAR_MAX);
    ui->cellPB_2->setMaximum(CELL_BAR_MAX);
    ui->cellPB_3->setMaximum(CELL_BAR_MAX);
    ui->cellPB_4->setMaximum(CELL_BAR_MAX);
    ui->cellPB_5->setMaximum(CELL_BAR_MAX);
    ui->cellPB_6->setMaximum(CELL_BAR_MAX);
    ui->cellPB_7->setMaximum(CELL_BAR_MAX);
    ui->cellPB_8->setMaximum(CELL_BAR_MAX);

    pl8 = new pl8Comm();

    // read presets - try 4 times
    int i=4;
    while(i || pl8->readPresets())
        i--;

    pl8->startPolling(POLLING_INTERVAL);
    connect(pl8, SIGNAL(ramDataAvailable()), this, SLOT(updateValues()));
}

MainWindow::~MainWindow()
{
    delete pl8;
    delete ui;
}


void MainWindow::updateValues()
{
    this->setWindowTitle(QString("PowerLab8 (v2) Control SW - Charger %1, FW ver %2").arg(pl8->getChargerID()).arg(pl8->getFwVersion()));

    float  cellVolts[8] = {0};
    float  cellIR[8] = {0};
    quint8 cellBalance[8] = {0};
    quint16 cellVoltsBar[8] = {0};

    pl8->getCellVolt(&cellVolts[0]);
    pl8->getCellInternalResistence(&cellIR[0]);
    pl8->getBalancerPWM(&cellBalance[0]);

    for(int i=0; i<8; i++)
    {
        cellVoltsBar[i] = MAX(MIN(cellVolts[i]*1000,CELL_BAR_MAX),CELL_BAR_MIN);
    }

    ui->cellPB_1->setValue(cellVoltsBar[0]);
    ui->cellPB_2->setValue(cellVoltsBar[1]);
    ui->cellPB_3->setValue(cellVoltsBar[2]);
    ui->cellPB_4->setValue(cellVoltsBar[3]);
    ui->cellPB_5->setValue(cellVoltsBar[4]);
    ui->cellPB_6->setValue(cellVoltsBar[5]);
    ui->cellPB_7->setValue(cellVoltsBar[6]);
    ui->cellPB_8->setValue(cellVoltsBar[7]);

    ui->cell_VVal_1->setText(QString("%1v").arg(cellVolts[0],0,'f',3));
    ui->cell_VVal_2->setText(QString("%1v").arg(cellVolts[1],0,'f',3));
    ui->cell_VVal_3->setText(QString("%1v").arg(cellVolts[2],0,'f',3));
    ui->cell_VVal_4->setText(QString("%1v").arg(cellVolts[3],0,'f',3));
    ui->cell_VVal_5->setText(QString("%1v").arg(cellVolts[4],0,'f',3));
    ui->cell_VVal_6->setText(QString("%1v").arg(cellVolts[5],0,'f',3));
    ui->cell_VVal_7->setText(QString("%1v").arg(cellVolts[6],0,'f',3));
    ui->cell_VVal_8->setText(QString("%1v").arg(cellVolts[7],0,'f',3));

    ui->cell_IR_1->setText(QString("%1mohm").arg(cellIR[0],0,'f',1));
    ui->cell_IR_2->setText(QString("%1mohm").arg(cellIR[1],0,'f',1));
    ui->cell_IR_3->setText(QString("%1mohm").arg(cellIR[2],0,'f',1));
    ui->cell_IR_4->setText(QString("%1mohm").arg(cellIR[3],0,'f',1));
    ui->cell_IR_5->setText(QString("%1mohm").arg(cellIR[4],0,'f',1));
    ui->cell_IR_6->setText(QString("%1mohm").arg(cellIR[5],0,'f',1));
    ui->cell_IR_7->setText(QString("%1mohm").arg(cellIR[6],0,'f',1));
    ui->cell_IR_8->setText(QString("%1mohm").arg(cellIR[7],0,'f',1));

    ui->cell_Bal_1->setText(QString("%1%").arg((quint16)cellBalance[0]*100/31));
    ui->cell_Bal_2->setText(QString("%1%").arg((quint16)cellBalance[1]*100/31));
    ui->cell_Bal_3->setText(QString("%1%").arg((quint16)cellBalance[2]*100/31));
    ui->cell_Bal_4->setText(QString("%1%").arg((quint16)cellBalance[3]*100/31));
    ui->cell_Bal_5->setText(QString("%1%").arg((quint16)cellBalance[4]*100/31));
    ui->cell_Bal_6->setText(QString("%1%").arg((quint16)cellBalance[5]*100/31));
    ui->cell_Bal_7->setText(QString("%1%").arg((quint16)cellBalance[6]*100/31));
    ui->cell_Bal_8->setText(QString("%1%").arg((quint16)cellBalance[7]*100/31));

    ui->modeLabel->setText(pl8->getModeName());
    ui->chemistryLabel->setText(pl8->getChemistryName());
    ui->presetLabel->setText(pl8->getPresetName(pl8->getLoadedPresetNumber()));
    ui->cellsLable->setText(tr("%1 Cells").arg(pl8->getDetectedCellCount()));
    ui->cycleLable->setText(tr("Cycle #%1").arg(pl8->getCycleNumber()));

    if(pl8->isReduceAmps())
    {
        ui->generalNote->setText(pl8->getPowerReducedReasonName());
    }

}


void MainWindow::on_pushButton_clicked()
{
    if(pl8->getMode() == MODE_CHARGER_READY_TO_START)
    {
        pl8->stopPolling();
        pl8->chargerOperation(OPR_START_DISCHARGE);
        pl8->restartPolling();
    }
    else
    {
        pl8->stopPolling();
        pl8->chargerStop();
        pl8->restartPolling();
    }


//    pl8->readPresets();
//    pl8->readRam();
//    updateValues();
}
